package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.wemedia.dtos.WmchannelDto;
import com.heima.model.common.wemedia.pojos.WmChannel;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
public class WmchannelController {

    @Autowired
    private WmChannelService wmChannelService;

    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<WmChannel> list = wmChannelService.findAll();
        return ResponseResult.okResult(list);
    }

    @GetMapping("del/{id}")
    public ResponseResult del(@PathVariable Integer id){
        wmChannelService.removeById(id);
        return ResponseResult.okResult(200, "删除成功！");
    }

    @PostMapping("list")
    public ResponseResult list(@RequestBody WmchannelDto wmchannelDto){

        return ResponseResult.okResult(wmChannelService.listAndLike(wmchannelDto));
    }

    @PostMapping("save")
    public ResponseResult save(@RequestBody WmChannel wmchannel){

        wmchannel.setCreatedTime(new Date());
        return ResponseResult.okResult(wmChannelService.save(wmchannel));
    }

    @PostMapping("update")
    public ResponseResult update(@RequestBody WmChannel wmChannel){

        return ResponseResult.okResult(wmChannelService.updateById(wmChannel));
    }
}